package com.sirui.x.learn_security.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import javax.annotation.Resource;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

@Component
public class UserDetailServiceImpl implements UserDetailsService {

	@Resource
	private PasswordEncoder passwordEncoder;
	
	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		
		if(!Objects.equals("admin", username)) {// AuthenticationException子类，可被spring security处理
			throw new UsernameNotFoundException("用户不存在");
		}
		
		List<GrantedAuthority> list = new ArrayList<GrantedAuthority>();
		list.add(() -> "perms:test1");
		return new User(username, passwordEncoder.encode(username), list);
	}
}
